package main

import "fmt"

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */

type ListNode struct {
	Val  int
	Next *ListNode
}

func createLine(listVal []int, pos int) *ListNode {
	var lnList []*ListNode
	lnLast := &ListNode{}
	for _, val := range listVal {
		lnCur := &ListNode{Val: val}
		lnList = append(lnList, lnCur)
		lnLast.Next = lnCur
		lnLast = lnCur
	}
	if pos >= 0 { // pos 如题目所述，一定小于列表长度
		lnLast.Next = lnList[pos]
	}
	if len(listVal) == 0 {
		return lnLast.Next
	}
	return lnList[0]
}

func hasCycle(head *ListNode) bool {
	if head == nil || head.Next == nil {
		return false
	}
	slow, fast := head, head.Next
	for fast != slow {
		if fast == nil || fast.Next == nil {
			return false
		}
		slow, fast = slow.Next, fast.Next.Next
	}
	return true
}

func main() {
	fmt.Println(hasCycle(createLine([]int{3, 2, 0, -4}, 1)))
}
